using FlickrNet;
using System;
using System.ComponentModel;
using WindowsLive.Writer.Api;

namespace FlickrHarmony {
    public class Profile {
		IProperties _settings;

		public Profile(IProperties pluginSettings) { _settings = pluginSettings; }

		/// <summary>
		/// The image size to insert into the blog posting
		/// </summary>
		public ImageSize ImageSize {
			get { return (_settings.GetInt("ImageSize", (int)ImageSize.Large1024)).ToEnum<ImageSize>(); }
			set { _settings.SetInt("ImageSize", (int)value); }
		}

		/// <summary>
		/// The HTML pattern to use when inserting images into the blog posting
		/// </summary>
		/// <remarks>
		/// The pattern should include {url} and {title} for replacement
		/// </remarks>
		public string ImagePattern {
			get { return _settings.GetString("HtmlPattern", string.Empty); }
			set { _settings.SetString("HtmlPattern", value); }
		}

        public string UserName {
            get {
				var name = _settings.GetString("FlickrUserName", string.Empty);
				if (string.IsNullOrEmpty(name)) {
					// lookup user details if we don't have them
					var login = Plugin.Flickr.TestLogin();
					if (login != null) {
						this.UserName = name = login.UserName;
						this.UserID = login.UserId;
					}
				}
				return name;
			}
            set { _settings.SetString("FlickrUserName", value); }
        }
        public string UserID {
            get {
				var id = _settings.GetString("FlickrUserId", string.Empty);
				if (string.IsNullOrEmpty(id)) {
					// lookup user details if we don't have them
					var login = Plugin.Flickr.TestLogin();
					if (login != null) {
						this.UserName = login.UserName;
						this.UserID = id = login.UserId;
					}
				}
				return id;
			}
            set { _settings.SetString("FlickrUserId", value); }
        }

		/// <summary>
		/// Authentication token supplied by Flickr
		/// </summary>
        public string Token {
            get { return _settings.GetString("FlickrToken", string.Empty); }
            set { _settings.SetString("FlickrToken", value); }
        }

		/// <summary>
		/// Whether or not to show thumbnails with the photo information retrieved
		/// from Flickr
		/// </summary>
		public bool ShowThumbnails {
			get { return _settings.GetBoolean("ShowThumbnails", false); }
			set { _settings.SetBoolean("ShowThumbnails", value); }
		}

		/// <summary>
		/// When validating images, should the anchor "alt" attribute be
		/// updated with the current Flickr title
		/// </summary>
		public bool ReplaceAltWithTitle {
			get { return _settings.GetBoolean("ReplaceAltWithTitle", false); }
			set { _settings.SetBoolean("ReplaceAltWithTitle", value); }
		}

		/// <summary>
		/// Indicates if token is present not if it is still authorized at Flickr
		/// </summary>
		public bool IsEmptyToken { get { return string.IsNullOrEmpty(this.Token); } }
    }
}